/**
 * 
 */
package com.coolbi.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Clark
 * 
 */
public class ValidationCodeFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String verifycode = req.getParameter("verifycode");
		
		if (verifycode == null || verifycode.trim().equals("")) {
			res.sendRedirect(req.getContextPath()+"/admin/login.jsp");
			return;
		}

		String rand = (String) req.getSession().getAttribute("rand");
		req.getSession().removeAttribute("rand");
		if (!verifycode.equalsIgnoreCase(rand)) {
			res.sendRedirect(req.getContextPath()+"/admin/login.jsp");
			return;
		}
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig config) throws ServletException {

	}

}
